package com.workflow.engine.demo.utils;

import com.alibaba.fastjson.JSON;
import com.workflow.engine.demo.service.entity.vo.InsurancePackageVO;
import com.workflow.engine.demo.service.entity.vo.WangXiaoResultVO;
import com.workflow.engine.demo.service.entity.vo.WangXiaoResultVO.Data.Quote.InsType;
import org.slf4j.Logger;

import java.text.ParseException;
import java.util.Iterator;
import java.util.List;

import static com.workflow.engine.core.utils.DateUtil.format;
import static com.workflow.engine.core.utils.DateUtil.parse;

/**
 * 公共的业务工具类
 * Created by houjinxin on 16/3/18.
 */
public class BusinessUtil {

    /**
     * 检查交强险是否选中
     *
     * @param insurancePackage
     * @return
     */
    public static boolean checkCompulsoryIsChosen(InsurancePackageVO insurancePackage) {
        return insurancePackage.isCompulsory() && insurancePackage.isAutoTax();
    }

    /**
     * 用于创建险种的报价详情对象
     *
     * @param amount
     * @param code
     * @param premium
     * @param original
     * @return
     */
    public static InsType.BizInfo.Detail createDetail(String code, String name, String amount, String premium, String original) {
        InsType.BizInfo.Detail detail = new InsType.BizInfo.Detail();
        detail.setCode(code);
        detail.setName(name);
        detail.setAmount(amount);
        detail.setPremium(premium);
        detail.setOriginal(original);
        return detail;
    }

    /**
     * 为报价结果设置保险公司名称
     *
     * @param logger
     * @param quote
     * @param supplier_name
     * @param supplier_code
     * @return 报价结果
     */
    @Deprecated
    public static WangXiaoResultVO.Data.Quote checkQuoteResult(Logger logger, WangXiaoResultVO.Data.Quote quote, String supplier_name, String supplier_code) {
        quote.setSupplierCode(supplier_code);
        quote.setSupplierName(supplier_name);
        logger.info("{}报价结果如下:\n{}", supplier_name, JSON.toJSON(quote));
        return quote;
    }

    /**
     * 创建失败的报价对象
     *
     * @param serviceName 保险公司服务名
     * @param msgEnum     失败信息
     * @return
     */
    public static WangXiaoResultVO.Data.Quote createFailQuote(String serviceName, MsgEnum msgEnum) {
        WangXiaoResultVO.Data.Quote quote = new WangXiaoResultVO.Data.Quote();
        InsType insType = new InsType();
        InsType.BizInfo bizInfo = new InsType.BizInfo();
        InsType.Force force = new InsType.Force();
        insType.setBizInfo(bizInfo);
        insType.setForceInfo(force);
        quote.setInsType(insType);
        quote.setCode(msgEnum.getCode());
        quote.setMsg(msgEnum.getMsg());
        setSupplierInfoForQuote(serviceName, quote);
        return quote;
    }

    /**
     * 创建包含某一步骤返回信息的Quote对象
     *
     * @param serviceName
     * @param msgEnum
     * @param stepInfo
     * @return
     */
    public static WangXiaoResultVO.Data.Quote createQuoteByStepInfo(String serviceName, MsgEnum msgEnum, Object stepInfo) {
        WangXiaoResultVO.Data.Quote quote = new WangXiaoResultVO.Data.Quote();
        quote.setStepInfo(stepInfo);
        quote.setCode(msgEnum.getCode());
        quote.setMsg(msgEnum.getMsg());
        setSupplierInfoForQuote(serviceName, quote);
        return quote;
    }

    /**
     * 设置保险公司信息,不管是成功的或者失败的报价都会用到这个方法.
     *
     * @param serviceName 保险公司服务类名
     * @param quote       报价结果对象
     */
    public static void setSupplierInfoForQuote(String serviceName, WangXiaoResultVO.Data.Quote quote) {
        if (serviceName.contains("Picc")) {
            quote.setSupplierName(com.workflow.engine.demo.picc.config.Constants.SUPPLIER_NAME);
            quote.setSupplierCode(com.workflow.engine.demo.picc.config.Constants.SUPPLIER_CODE);
        } else if (serviceName.contains("Pingan")) {
            quote.setSupplierName(com.workflow.engine.demo.pingan.config.Constants.SUPPLIER_NAME);
            quote.setSupplierCode(com.workflow.engine.demo.pingan.config.Constants.SUPPLIER_CODE);
        } else {
        	quote.setSupplierName("其他");
            quote.setSupplierCode("default");
        }
    }

    /**
     * 删除结果中未报出价的险种, List是引用传递,方法不需要返回值
     *
     * @param details
     */
    public static void filterNullPremiumInDetail(List<InsType.BizInfo.Detail> details) {
        //删除一个List中的元素的方法有两种
        //1.用迭代器的remove方法删除。
        //2.从后面删除
//        for (int i = details.size() - 1; i >= 0; i--) {
//            Detail detail = details.get(i);
//            if (detail.getPremium() == null || "".equals(detail.getPremium())) {
//                details.remove(detail);
//            }
//        }
        Iterator<InsType.BizInfo.Detail> it = details.iterator();
        while (it.hasNext()) {
            InsType.BizInfo.Detail detail = it.next();
            if (detail.getPremium() == null || "".equals(detail.getPremium())) {
                it.remove();
            }
        }
    }

    /**
     * 获取指定格式的字符串型生日
     *
     * @param identity
     * @return
     */
    public static String getBirthdayFromIdentity(String identity, String pattern) throws ParseException {
        String birthDay;
        if (identity.length() == 15) {
            birthDay = "19" + identity.substring(6, 12);
        } else {
            birthDay = identity.substring(6, 14);
        }
        return format(parse(birthDay, "yyyyMMdd"), pattern);
    }

    /**
     * 获取性别,各个保险公司性别对应的值不一样,故后两个参数分别表示保险公司对应的男女标识
     *
     * @param identity 身份证
     * @param male 男性标识
     * @param female 女性标识
     * @return
     */
    public static String getGenderFromIdentity(String identity, String male, String female) {
        String gender;
        if (identity.length() == 15) {
            gender = Integer.valueOf(identity.substring(14, 15)) % 2 == 0 ? female : male;
        } else {
            gender = Integer.valueOf(identity.substring(16, 17)) % 2 == 0 ? female : male;
        }
        return gender;
    }


    /**
     * 获取身份证后六位
     *
     * @param identity
     * @return
     */
    public static String getLastSixNumberFromIdentity(String identity) {
        String lastSixNumber;
        if (identity.length() == 15) {
            lastSixNumber = identity.substring(9, 15);
        } else {
            lastSixNumber = identity.substring(12, 18);
        }
        return lastSixNumber;
    }

    /**
     * 四舍五入方法,可以指定精度
     *
     * @param original
     * @param radix
     * @return
     */
    public static double round(double original, int radix) {
        double factor = Math.pow(10.0, radix); //这里一定是10.0 否则会按照整形计算
        return Math.round(original * factor) / factor;
    }

}
